package com.fengkai.filepost.pdfoutstream.test.ODsolutions;

import com.alibaba.druid.sql.visitor.functions.Char;

import java.util.Stack;

/**
 * @author Fengkai
 * @creat 2023/4/12
 * 输入描述:
 * 一个只包括 ‘(‘，’)’，'{‘，’}’，'[‘，’]’的字符串
 * 输出描述:
 * 一个整数，最大的括号深度
 * 示例1  输入输出示例仅供调试，后台判题数据一般不包含示例
 * 输入
 * []
 * 输出
 * 1
 * 说明
 * 有效字符串，最大嵌套深度为1
 */
public class FindKuoHao {
    public static void main (String[] args) {
        System.out.println(findKuohao("([]{()}))"));

    }


    //用栈做
    public static int findKuohao (String s) {
        Stack<Character> characters = new Stack<>();
        int i = 0;
        int sum = 0;
        for (; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                characters.push(c);
                sum = Math.max(sum, characters.size());
            } else {
                if (characters.size() == 0) {
                    break;
                }
                if (c == ')') {
                    if (characters.pop() == '(') {
                        continue;
                    }
                } else if (c == ']') {
                    if (characters.pop() == '[') {
                        continue;
                    }
                } else if (c == '}') {
                    if (characters.pop() == '{') {
                        continue;
                    }
                }
                break;
            }
        }
        if (i == s.length() && characters.size() == 0) {
            return sum;
        } else {
            return 0;
        }
    }
}
